/*
 * ImageButton.cpp
 *
 *  Created on: 2012-10-20
 *      Author: yangentao@gmail.com
 */

#include "ImageButton.h"
#include <atlimage.h>

class AtlImage{
public:
	ATL::CImage image;
};

ImageButton::ImageButton() {
	status = bsNormal;
	transparentColor = -1;
	InnerEvents.OnPaint = boost::bind(&ImageButton::onPaint, this, _1, _2, _3);
	InnerEvents.OnMouseEnter = boost::bind(&ImageButton::onMouseEnter, this, _1);
	InnerEvents.OnMouseLeave = boost::bind(&ImageButton::onMouseLeave, this, _1);
	InnerEvents.OnLeftButtonDown = boost::bind(&ImageButton::onLeftButtonDown, this, _1);
	InnerEvents.OnLeftButtonUp = boost::bind(&ImageButton::onLeftButtonUp, this, _1);
	Super::addMessageListener(&InnerEvents);
	Super::setAutoCapture(false);

	imageNormal.reset(new AtlImage());
	imageMouseEnter.reset(new AtlImage());
	imageMouseDown.reset(new AtlImage());

//	imageBorder.Load(L"D:\\soy\\soy\\soy\\res\\border.9.png");
}

ImageButton::~ImageButton() {
}
bool ImageButton::create(HWND parent, Rect rect) {
	return Super::create(parent, rect);
}
bool ImageButton::loadImage(ImageButtonParam images) {
	setImageMouseEnter(images.imageMouseEnter);
	setImageMouseDown(images.imageMouseDown);
	return setImageNormal(images.imageNormal);
}
bool ImageButton::setImageNormal(String imageFile) {
	if (imageFile.empty()) {
		return false;
	}
	WideText tw(imageFile.buffer(), imageFile.length());
	return SUCCEEDED(imageNormal->image.Load(tw.buf));
}
bool ImageButton::setImageMouseEnter(String imageFile) {
	if (!imageFile.empty()) {
		WideText tw(imageFile.buffer(), imageFile.length());
		return SUCCEEDED(imageMouseEnter->image.Load(tw.buf));
	}
	return false;
}
bool ImageButton::setImageMouseDown(String imageFile) {
	if (!imageFile.empty()) {
		WideText tw(imageFile.buffer(), imageFile.length());
		return SUCCEEDED(imageMouseDown->image.Load(tw.buf));
	}
	return false;
}

void ImageButton::setTransparentColor(LONG color) {
	transparentColor = color;
}

int ImageButton::getImageWidth() {
	if (imageNormal->image.IsNull()) {
		return 0;
	}
	return imageNormal->image.GetWidth();
}
int ImageButton::getImageHeight() {
	if (imageNormal->image.IsNull()) {
		return 0;
	}
	return imageNormal->image.GetHeight();
}

bool ImageButton::onMouseEnter(const MouseEvent& me) {
	status = bsMouseEnter;
	Super::invalidate(true);
	return true;
}
bool ImageButton::onMouseLeave(const MouseEvent& me) {
	status = bsNormal;
	Super::invalidate(true);
	return true;
}
bool ImageButton::onLeftButtonDown(const MouseEvent& me) {
	status = bsMouseDown;
	Super::invalidate(true);
	return true;
}
bool ImageButton::onLeftButtonUp(const MouseEvent& me) {
	status = bsMouseEnter;
	Super::invalidate(true);
	return true;
}

void ImageButton::onPaint(HWND, HDC dc, PAINTSTRUCT*) {
	ATL::CImage* img = &imageNormal->image;
	if (status == bsMouseDown) {
		if (!imageMouseDown->image.IsNull()) {
			img = &imageMouseDown->image;
		} else {
			if (!imageMouseEnter->image.IsNull()) {
				img = &imageMouseEnter->image;
			}
		}
	}
	if (status == bsMouseEnter) {
		if (!imageMouseEnter->image.IsNull()) {
			img = &imageMouseEnter->image;
		} else {
			if (!imageMouseDown->image.IsNull()) {
				img = &imageMouseDown->image;
			}
		}
	}

	Rect r = getClientRect();
	Canvas g(dc);

	if (status == bsMouseEnter || status == bsMouseDown) {
		g.selectPenColor(Color(184,214,251));
		g.roundRectangle(r, 6, 6);
//			if (!imageBorder.IsNull()) {
//				imageBorder.Draw(dc, 0, 0, r.Width() , r.Height() );
//			} else {
//				Log << "Load Border Image Failed!" << endl;
//			}
	}
	Rgn rgn;
	Rect rClip = r;
	rClip.zoom(-1);
	rgn.createRoundRectRgn(rClip, 5, 5);
	g.clipRgn(rgn);



	if (!img->IsNull()) {
		if (transparentColor >= 0) {
			img->TransparentBlt(dc, 1, 1, r.Width() - 2, r.Height() - 2, transparentColor & 0x00FFFFFF);
		} else {
			img->Draw(dc, 1, 1, r.Width() - 2, r.Height() - 2);
		}
	}

}
